//Page Object
const url = require('../../config/config.js')
Page({
    data: {

    },
    // 用于一键登录
    aseVo: {
        code: "",
        iv: "",
        encryptedData: "",
        rawData: "",
        signature: "",
    },


    //options(Object)
    onLoad: function() {
        // const token = wx.getStorageSync("token");
        // if (token) {
        //     wx.switchTab({
        //         url: '/pages/index/index'
        //     });
        // }
        var obj = wx.getLaunchOptionsSync()
        console.log('启动小程序的路径:', obj.path)
        console.log('启动小程序的场景值:', obj.scene)
        console.log('启动小程序的 query 参数:', obj.query)
        console.log('来源信息:', obj.shareTicket)
        console.log('来源信息参数appId:', obj.referrerInfo.appId)
        console.log('来源信息传过来的数据:', obj.referrerInfo.extraData)
        if (obj.scene == 1001) {
            // 用于判断是不是有没有授权           
            const userInfo = wx.getStorageSync("userInfo");
            if (userInfo) {
                // 存在即调用
                this.login()
            } else {
                // 不存在直接让他先登录
                wx.navigateTo({
                    url: '/pages/login/index',
                });
            }
        }

    },
    //  login情求 这个成功调用
    login() {
        wx.login({
            timeout: 10000,
            success: (result) => {
                console.log(result)
                this.aseVo.code = result.code;
                this.getUserInfo()

            }
        });
    },
    // 检测用户的session_key
    handleSession_key() {
        wx.checkSession({
            success: (result) => {
                console.log(result)
            },
            fail: (fail) => {
                console.log(fail)
            }
        });
    },
    // 获取一下用户的信息
    getUserInfo() {
        console.log(22)
        wx.getUserInfo({
            withCredentials: 'false',
            lang: 'zh_CN',
            timeout: 10000,
            success: (result) => {
                console.log(result)
                this.aseVo.rawData = result.rawData;
                this.aseVo.iv = result.iv;
                this.aseVo.signature = result.signature;
                this.aseVo.encryptedData = result.encryptedData;
                this.oneClickLogin()


            }
        });
    },
    // 用于微信公众号模板 一键登录
    oneClickLogin() {
        wx.request({
            url: url.b.login,
            data: { aseVo: JSON.stringify(this.aseVo) },
            header: { "Content-Type": "application/x-www-form-urlencoded" },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                console.log(result)

            }
        });
    }

});